<?php
namespace BP\Sys;
require_once("BP/En/Map.php");
class GEEntityMyPK extends Entity {
	public $FK_MapData;	// String
	protected $_Dtls;	// ArrayList
	private function __init() { // default class members
		$this->FK_MapData = NULL;
		$this->_Dtls = NULL;
	}
	public function getMyPK () 
	{
		return $this->GetValStrByKey("MyPK");
	}
	public function setMyPK ($value) // [String value]
	{
		$this->SetValByKey("MyPK", $value);
	}
	public function getPK () 
	{
		return "MyPK";
	}
	public function getPKField () 
	{
		return "MyPK";
	}
	public function toString () 
	{
		return $this->FK_MapData;
	}
	public function getClassID () 
	{
		return $this->FK_MapData;
	}
	public static function constructor__ () 
	{
		$me = new self();
		$me->__init();
		return $me;
	}
	public static function constructor__String ($fk_mapdata) // [String fk_mapdata]
	{
		$me = new self();
		$me->__init();
		$me->FK_MapData = $fk_mapdata;
		return $me;
	}
	public static function constructor__String_String ($fk_mapdata, $pk) // [String fk_mapdata, String pk]
	{
		$me = new self();
		$me->__init();
		$me->FK_MapData = $fk_mapdata;
		$me->setPKVal($pk);
		$me->Retrieve();
		return $me;
	}
	public function getEnMap () 
	{
		if (($this->get_enMap() != NULL))
		{
			return $this->get_enMap();
		}
		if (($this->FK_MapData == NULL))
		{
			throw new \RuntimeException((("没有�?" . $this->FK_MapData) . "值，您不能获取它的Map�?"));
		}
		$this->set_enMap(MapData->GenerHisMap($this->FK_MapData));
		return $this->get_enMap();
	}
	public function getGetNewEntities () 
	{
		if (($this->FK_MapData == NULL))
		{
			return GEEntityMyPKs::constructor__();
		}
		return GEEntityMyPKs::constructor__String($this->FK_MapData);
	}
	public function CopyFromFrm ($en) // [GEEntityMyPK en]
	{
		$oldOID = $this->getMyPK();
		$this->Copy($en);
		$this->Save();
		$this->setMyPK($oldOID);
		$dtls = MapDtls::constructor__String($this->FK_MapData);
		$dtlsFrom = MapDtls::constructor__String($en->FK_MapData);
		if (($dtls->size() != $dtls->size()))
		{
			throw new \RuntimeException("@复制的两个表单从表不�?�?...");
		}
		$i = 0;
		foreach ($dtls->ToJavaList() as $dtl) 		{
			DBAccess::RunSQL(((("DELETE FROM " . $dtl->getPTable()) . " WHERE RefPK=") . $this->getMyPK()));
			$dtlFrom = ( ($dtlsFrom->get($i) instanceof MapDtl) ? $dtlsFrom->get($i) : NULL );
			$ensDtlFrom = GEDtls::constructor__String($dtlFrom->getNo());
			$ensDtlFrom->Retrieve(GEDtlAttr::$RefPK, $oldOID);
			$dtlEnBlank = GEDtl::constructor__String($dtl->getNo());
			foreach ($ensDtlFrom->ToJavaList() as $enDtlFrom) 			{
				$dtlEnBlank->Copy($enDtlFrom);
				$dtlEnBlank->setRefPK($this->getMyPK());
				$dtlEnBlank->SaveAsNew();
			}
			++$i;
		}
		$aths = FrmAttachments::constructor__String($this->FK_MapData);
		$athsFrom = FrmAttachments::constructor__String($en->FK_MapData);
		foreach ($aths->ToJavaList() as $ath) 		{
			DBAccess::RunSQL((((("DELETE FROM Sys_FrmAttachmentDB WHERE FK_MapData='" . $this->FK_MapData) . "' AND RefPKVal='") . $this->getMyPK()) . "'"));
			foreach ($athsFrom->ToJavaList() as $athFrom) 			{
				if (!$athFrom->getNoOfObj()->equals($ath->getNoOfObj()))
				{
					continue;
				}
				$athDBsFrom = FrmAttachmentDBs::constructor__();
				$athDBsFrom->Retrieve(FrmAttachmentDBAttr::$FK_FrmAttachment, $athFrom->getMyPK(), FrmAttachmentDBAttr::$RefPKVal, $en->getMyPK());
				foreach ($athDBsFrom->ToJavaList() as $athDBFrom) 				{
					$athDBFrom->setMyPK(DBAccess->GenerGUID());
					$athDBFrom->setFK_FrmAttachment($ath->getMyPK());
					$athDBFrom->setRefPKVal($this->getMyPK());
					$athDBFrom->Insert();
				}
			}
		}
	}
	public function CopyToOID ($oid) // [String oid]
	{
		$oidOID = $this->getMyPK();
		$this->setMyPK($oid);
		$this->Save();
		$dtls = MapDtls::constructor__String($this->FK_MapData);
		foreach ($dtls->ToJavaList() as $dtl) 		{
			DBAccess::RunSQL(((("DELETE FROM " . $dtl->getPTable()) . " WHERE RefPK=") . $this->getMyPK()));
			$ensDtl = GEDtls::constructor__String($dtl->getNo());
			$ensDtl->Retrieve(GEDtlAttr::$RefPK, $oidOID);
			foreach ($ensDtl->ToJavaList() as $enDtl) 			{
				$enDtl->setRefPK($this->getMyPK());
				$enDtl->InsertAsNew();
			}
		}
		$aths = FrmAttachments::constructor__String($this->FK_MapData);
		foreach ($aths->ToJavaList() as $ath) 		{
			DBAccess::RunSQL((((("DELETE FROM Sys_FrmAttachmentDB WHERE FK_MapData='" . $this->FK_MapData) . "' AND RefPKVal='") . $this->getMyPK()) . "'"));
			$athDBs = FrmAttachmentDBs::constructor__String_String($this->FK_MapData, $oidOID);
			foreach ($athDBs->ToJavaList() as $athDB) 			{
				$athDB_N = FrmAttachmentDB::constructor__();
				$athDB_N->Copy($athDB);
				$athDB_N->setFK_MapData($this->FK_MapData);
				$athDB_N->setRefPKVal($this->getMyPK());
				if (($athDB_N->getHisAttachmentUploadType() == AttachmentUploadType::$Single))
				{
					$athDB_N->setMyPK((($athDB_N->getFK_FrmAttachment() . "_") . $this->getMyPK()));
					if (($athDB_N->getIsExits() ==  TRUE ))
					{
						continue;
					}
					$athDB_N->Insert();
				}
				else
				{
					$athDB_N->setMyPK(DBAccess->GenerGUID());
					$athDB_N->Insert();
				}
			}
		}
	}
	public function getDtls () 
	{
		if (($this->_Dtls == NULL))
		{
			$this->_Dtls = [];// 
		}
		return $this->_Dtls;
	}
}
?>
